package zmee.pur.tnd.formplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

import static kd.bos.orm.query.QCP.equals;

/**
 * @Author: xw
 * @Date: 2022/11/11/10:28
 * @Description: 寻源管理：供应商默认已选
 */
public class SupplierF7Plugin extends AbstractFormPlugin implements BeforeF7SelectListener {
    /**
     * 寻源项目标识
     */
    private static final String KEY_PROJECT = "project";

    /**
     * 供应商范围标识
     */
    private static final String KEY_SUPPLIERSCOPE = "supplierscope";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        BasedataEdit fieldEdit2 = this.getView().getControl(KEY_SUPPLIERSCOPE);
        fieldEdit2.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        //供应商范围默认选择
        if (StringUtils.equals(name, KEY_SUPPLIERSCOPE)) {
            Object value = this.getModel().getValue(KEY_PROJECT);
            if (null != value) {
                Object[] entryIds = getTheBiddingSupplier((DynamicObject) value);
                if (entryIds == null) {
                    return;
                }

                //设置供应商已选
                ListShowParameter showParameter = (ListShowParameter) event.getFormShowParameter();
                showParameter.setSelectedRows(entryIds);
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        Object value = this.getModel().getValue(KEY_PROJECT);
        if (null != value) {
            Object[] entryIds = getTheBiddingSupplier((DynamicObject) value);
            if (entryIds == null) {
                return;
            }
            List<Object> ids = new ArrayList<>();
            for (int i = 0; i < entryIds.length; i++) {
                if (entryIds[i] == null) {
                    continue;
                }
                ids.add(entryIds[i]);
            }

            Map<Object, DynamicObject> supplierMaps = BusinessDataServiceHelper.loadFromCache(ids.toArray(), "bd_supplier");
            //设置供应商已选
            this.getModel().setValue(KEY_SUPPLIERSCOPE, supplierMaps);
        }
    }

    private Object[] getTheBiddingSupplier(DynamicObject value) {
        String billno = value.getString("billno");
        //通过寻源项目编号查询开标准备
        QFilter filter = new QFilter("billno", equals, billno);
        DynamicObject bidOpen = BusinessDataServiceHelper.loadSingle("src_bidopen", "id", new QFilter[]{filter});
        if (null == bidOpen) {
            return null;
        }
        //通过开标准备id查询回标信息分录供应商是否投标
        QFilter filter1 = new QFilter("parentid", equals, bidOpen.getPkValue());
        DynamicObject tenderInfo = BusinessDataServiceHelper.loadSingle("src_tenderinfo_summary", "id,entryentity,entryentity.supplier,entryentity.istender", new QFilter[]{filter1});
        if (null == tenderInfo) {
            return null;
        }
        DynamicObjectCollection entryEntity = tenderInfo.getDynamicObjectCollection("entryentity");
        //供应商id
        Object[] entryIds = new Object[entryEntity.size()];
        for (int i = 0; i < entryEntity.size(); i++) {
            DynamicObject object = entryEntity.get(i);
            //是否投标
            boolean istender = object.getBoolean("istender");
            if (istender) {
                entryIds[i] = object.getDynamicObject("supplier").getPkValue();
            }
        }

        return entryIds;
    }
}